<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class TypeController extends BaseController {

    /**
     * 菜单添加
     */
    public function menuadd(){
    	$rules = array(
    		array('mtitle','require','菜单标题不能为空！'),
    	);
    	$submenu = D(C('TABLE_SUBMENU'));
    	if(!$submenu->validate($rules)->create()){
			$this->error($submenu->getError(),'',1);
		}else{
			$classtype = I('post.classtype');
			$jumpaddres = I('post.jumpaddres');
			$submenu->mcrdate = date("Y-m-d H:i:s", time());
			$submenu->mtypeid = I("get.rootid");
			$submenu->position = menupostion(I('post.position'));
			$result = $submenu->add();
			if($result){
				if(strstr($jumpaddres,'http://e.hrtv.cn')==false && strstr($jumpaddres,'http://act.hrtv.cn')==false){
					$mtypeid = $submenu->field('mtypeid')->find($result);
					$submenu->jumpaddres = gettypeaddres($classtype,$result,$mtypeid['mtypeid']);
					$updateresult = $submenu->where(array('id'=>$result))->save();
					if($updateresult){
						$this->success('添加成功');
					}else{
						$this->error('更新跳转地址失败','',1);
					}
				}else{
					$this->success('添加成功');
				}
				
			}else{
				$this->error('添加失败','',1);
			}
		}
    }

    /**
     * 菜单编辑
     */
    public function menuedit($id=null){
    	$rules = array(
    		array('mtitle','require','菜单标题不能为空！'),
    	);
    	$id = htmlspecialchars($id);
    	if(!empty($id)){
    		$submenu = D(C('TABLE_SUBMENU'));
    		if(!$submenu->validate($rules)->create()){
    			$this->error($submenu->getError(),'',1);
    		}else{
    			if(session('userinfo')['id']!=28) $submenu->position = menupostion(I('post.position'));
    			$result = $submenu->where(array('id'=>$id))->save();
    			if($result !== false){
    				$this->success('编辑成功！',U('Index/menulist',array('id'=>I('get.rootid'))));
    			}else{
    				$this->error('没有更改任何数据或编辑失败！','',1);
    			}
    		}
    	}else{
    		$this->error('非法参数！','',1);
    	}
    }
    
    protected function getmtypeid($classtype,$id){
    	$submenu = D(C('TABLE_SUBMENU'));
    	return gettypeaddres($classtype,$id,$submenu->find($id)['mtypeid']);
    }
    
    /**
     * 添加华人码验证规则
     */
    protected function rules($is=true){
    	if($is){
    		$rules = array(
    				array('typename','require','标题不能为空！'),
    				//array('domainid','/^[0-9]{1,9}+$/','domainID只能是数字！',0,'regex',1),
    				//array('domainid','','domainID已经存在！',0,'unique',1),
    				array('epname','require','企业名称不能为空！'),
    		);
    	}else{
    		$rules = array(
    				array('typename','require','标题不能为空！'),
    				//array('domainid','/^[0-9]{1,9}+$/','domainid只能是数字！',0,'regex',1),
    				array('epname','require','企业名称不能为空！'),
    		);
    	}
    	return $rules;
    }

    /**
     * 栏目添加
     */
    public function typeadd(){

		$template = I('post.template');
    	$type = D(C('TABLE_TYPE'));
    	if(!$type->validate($this->rules(true))->create()){
    		$this->error($type->getError(),'',2);
    	}else{
    		$date  = date("Y-m-d H:i:s", time());
    		$title = I('post.typename');
    		$m = M('ehrcompany',null,'ODDH5_DB_CONFIG1');
    		$m->version 			= 11;
    		$m->addtime 			= $date;
    		$m->banner_link 		= '';
    		$m->banner_url 			= '';
    		$m->del_time 			= null;
    		$m->domain 				= '';
    		$m->ifdel 				= 0;
    		$m->title 				= $title;
    		$m->adv_json 			= '';
    		$m->logo_link 			= '';
    		$m->logo_url 			= '';
    		$m->template 			= 12;
    		$m->banner_time 		= 1;
    		$m->banner_name 		= 1;
    		$m->edittype 			= 1;
    		$m->ifshare 			= 1;
    		$m->sharename 			= null;
    		$m->create_user 		= session('userinfo')['username'];
    		$m->createid 			= session('userinfo')['id'];
    		$m->examine_status 		= 1;
    		$m->external_company 	= 0;
    		$m->nick_name 			= session('userinfo')['username'];
			$resultID = $m->add();
			
    		//$resultID = 123456;
			if($resultID){
				$type->plate_id = rtrim(I('post.plate_id'),',');
				$type->crdate = $date;
				$type->userid = session('userinfo')['id'];
				$result = $type->add();
				if($result){
					if($this->preinstall($result, $template)){
						//$this->success('添加成功');
						/*更新domainID，让旧版与新版的domainID同步一致*/
						$d = D(C('TABLE_TYPE'));
						$d->domainid = $resultID;
						$d->state = session('userinfo')['audit_ma'];
						$updateresult = $d->where(array('id'=>$result))->save();
						if($updateresult){
							$this->success('添加成功', U('Index/main'));
						}
					}else{
						$this->error('子分类添加失败','',1);
					}
					 
				}else{
					$this->error('添加失败','',1);
				}
			}
    	}
    }
    
    protected function getdata($sn,$field){
    	$d = D(C('TABLE_PUBLICTYPE'));
    	$res = $d->where(array('p_sn' => $sn))->find();
    	return $res[$field];
    }

    /**
     * 增加华人码时预设插入电影、善缘、广告分类
     */
    protected function preinstall($id, $template){

    	$insertdate = date("Y-m-d H:i:s", time());
    	$submenu = D(C('TABLE_SUBMENU'));
		$qiyeimg = '/IndexPublic/images/qiyethumb.jpg';
		$guangao = '/Uploads/2016-06-06/57551cf6135eb.jpg';
		$guangao_link = 'http://act.hrtv.cn/consumer?bottomType=5&ut_source_domain=#domainid#';
		$data_addition = array('mtitle'=>'内容板块','malias'=>'内容板块','classtype'=>1,'mtype'=>1,'msort'=>1,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>1,'jumpaddres'=>'#','thumbimg' => $qiyeimg);

		// 模板10预置分类
		$dataList10[] = array('mtitle'=>'产品故事','malias'=>'产品故事','classtype'=>1,'mtype'=>1,'msort'=>1,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>1,'jumpaddres'=>'#','thumbimg' => $qiyeimg);
		$dataList10[] = array('mtitle'=>'企业故事','malias'=>'企业故事','classtype'=>1,'mtype'=>2,'msort'=>2,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>1,'jumpaddres'=>'#','thumbimg' => $qiyeimg);
		$dataList10[] = array('mtitle'=>'特色文化','malias'=>'特色文化','classtype'=>1,'mtype'=>3,'msort'=>3,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>1,'jumpaddres'=>'#','thumbimg' => $qiyeimg);
		$dataList10[] = array('mtitle'=>'全球优品','malias'=>'全球优品','classtype'=>1,'mtype'=>4,'msort'=>4,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>1,'jumpaddres'=>'https://shop18934028.youzan.com/v2/showcase/homepage?alias=shzxsflk&redirect_count=2&sf=wx_sm','thumbimg' => $qiyeimg);

		//定义空变量
		$tpl = null;
		if($template == 'index10'){
			$dataList = $dataList10;
			$tpl = 1;
		}else if($template == 'index6' || $template == 'index8' || $template == 'index9'){
			$dataList[] = $data_addition;
		}else{
			$dataList[] = array('mtitle'=>'企业','malias'=>'企业','classtype'=>1,'mtype'=>1,'msort'=>1,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>1,'jumpaddres'=>'#','thumbimg' => $qiyeimg);
			$dataList[] = array('mtitle'=>'政府','malias'=>'政府','classtype'=>2,'mtype'=>1,'msort'=>2,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>4,'jumpaddres'=>C('WEBDOMAIN').'/Home/Index/list2/id/'.$id,'thumbimg'=>$this->getdata(3,'p_img'));
			$dataList[] = array('mtitle'=>'电影','malias'=>'电影','classtype'=>null,'mtype'=>1,'msort'=>3,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>3,'jumpaddres'=>$this->getdata(1,'p_link'),'thumbimg'=>$this->getdata(1,'p_img'));
			$dataList[] = array('mtitle'=>'商城','malias'=>'商城','classtype'=>null,'mtype'=>1,'msort'=>4,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>3,'jumpaddres'=>$this->getdata(2,'p_link'),'thumbimg'=>$this->getdata(2,'p_img'));
			$dataList[] = array('mtitle'=>'广告','malias'=>'广告','classtype'=>null,'mtype'=>1,'msort'=>5,'state'=>1,'mtypeid'=>$id,'mparent'=>0,'mcrdate'=>$insertdate,'position'=>2,'jumpaddres' => $guangao_link,'thumbimg' => $guangao);
		}

    	$result = $submenu->addAll($dataList);
    	if($result){
			$this->update_jumpval($id);
			if(empty($tpl)){
				/*获取是政府的ID号*/
				$parid = $submenu->where(array('mtypeid'=>$id,'classtype'=>2))->find()['id'];
				$dataList1[] = array('mtitle'=>'视频热点','malias'=>'视频热点','classtype'=>2,'mtype'=>1,'msort'=>1,'state'=>1,'mtypeid'=>$id,'mparent'=>$parid,'mcrdate'=>$insertdate,'position'=>null,'jumpaddres'=>null,'thumbimg'=>null);
				$dataList1[] = array('mtitle'=>'要闻政策','malias'=>'要闻政策','classtype'=>2,'mtype'=>1,'msort'=>2,'state'=>1,'mtypeid'=>$id,'mparent'=>$parid,'mcrdate'=>$insertdate,'position'=>null,'jumpaddres'=>null,'thumbimg'=>null);
				$dataList1[] = array('mtitle'=>'人文情怀','malias'=>'人文情怀','classtype'=>2,'mtype'=>1,'msort'=>3,'state'=>1,'mtypeid'=>$id,'mparent'=>$parid,'mcrdate'=>$insertdate,'position'=>null,'jumpaddres'=>null,'thumbimg'=>null);
				$res = $submenu->addAll($dataList1);
				if($res){
					return true;
				}else{
					return false;
				}
			}else{
				return true;
			}
    	}else{
			return false;
    	}
    }

	protected function update_jumpval($id){
		$submenu = D(C('TABLE_SUBMENU'));
		$res = $submenu->where(array('mtypeid'=>$id,'classtype'=>1))->select();
		if($res){
			foreach($res as $v){
				if($v['msort'] != 4){
					$this->up_jmp($v['id']);
				}
			}
		}
	}

	protected function up_jmp($id){
		$submenu = D(C('TABLE_SUBMENU'));
		$data['jumpaddres'] = C('WEBDOMAIN').'/Home/Index/list1/id/'.$id;
		$submenu->where(array('id'=>$id))->save($data);
	}
    /**
     * 栏目编辑
     */
    public function typeedit($id=null){
    	$id = htmlspecialchars($id);
    	if(!empty($id)){
    		$type = D(C('TABLE_TYPE'));
    		if(!$type->validate($this->rules(false))->create()){
    			$this->error($type->getError(),'',2);
    		}else{
				$type->plate_id = rtrim(I('post.plate_id'),',');
    			$type->crdate   = date("Y-m-d H:i:s", time());
    			//$type->userid = session('userinfo')['id'];
    			$result = $type->where(array('id'=>$id))->save();
    			if($result !== false){
    				$this->success('编辑成功！',U('Index/main'));
    			}else{
    				$this->error('编辑失败！','',1);
    			}
    		}	
    	}else{
    		$this->error('非法参数！','',1);
    	}
    }

    /**
     * 删除
     */
    public function delete($id=null, $table){
    	$id = htmlspecialchars($id);
    	if(!empty($id)){
    		$type = D($table);
    		$reult = $type->delete($id);
    		if($reult){
    			if(I('get.del')){
    				$submenu = D(C('TABLE_SUBMENU'));
    				$submenu->where(array('mtypeid'=>$id))->delete();
    			}
    			$this->success('删除成功，跳转......');
    		}else{
    			$this->error('删除失败！','',1);
    		}
    	}else{
    		$this->error('缺少参数，删除失败！','',1);
    	}
    }

    /**
     * 改变状态
     */
    public function changestate($id=null,$state=null,$table){
    	$id = htmlspecialchars($id);
    	$state = htmlspecialchars($state);
    	if(!isset($state)) $this->error('非法参数！','',1);
    	$state_result = $state?'0':'1';
    	if(!empty($id)){
    		$type = D($table);
    		$type->state = $state_result;
    		$reult = $type->where(array('id'=>$id))->save();
    		if($reult !== false){
    			//echo $_SERVER['HTTP_REFERER']; 上一个页面地址
    			//$this->success('操作成功，跳转......');
    			echo 1; //操作成功
    		}else{
    			//$this->error('操作失败！','',1);
    			echo 2; //操作失败
    		}
    	}else{
    		//$this->error('缺少参数，删除失败！','',1);
    		echo 0; //非法参数
    	}
    }
    

}